package com.block.datapush.service.filter;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.service.impl.ChannelCommonService;
import com.block.datapush.model.param.FilterHandlerParam;
import com.block.datapush.model.result.FilterHandlerResult;
import com.block.datapush.service.IDataFilterItemService;
import com.block.db.entity.ChannelBaseInfo;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.mapper.ChannelBaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FilterInChannelTypeArrayHandlerService implements IDataFilterItemService {

    @Autowired
    ChannelCommonService channelCommonService;

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberSourceInfo memberSourceInfo) {

        String userValue = memberSourceInfo.getChannelCode();

        ChannelBaseInfo baseInfoByCode = channelCommonService.getBaseInfoByCode(userValue);

        // 该用户的来源流量类型
        String flowRateType = baseInfoByCode.getFlowRateType();

        // 允许推送的流量类型
        String filterValue = handlerParam.getInChannelType();

        if (StrUtil.isEmpty(filterValue)){
            return FilterHandlerResult.success();
        }

        // 只允许推送
        List<String> valueList = JSON.parseArray(filterValue, String.class);
        if (CollUtil.isEmpty(valueList) || valueList.contains(flowRateType)){
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【filter】{}【value】{}", filterValue, flowRateType);
        return FilterHandlerResult.error("IN_CHANNLETRPE_ERROR", message);
    }
}
